﻿using System.Web.Mvc;
using ExChange.Model;
using System.Text;

namespace ExChange.Web.Controllers
{
    public class BaseController : Controller
    {
        /// <summary>
        /// 登录管理员信息
        /// </summary>
        public Model.Admin LoginAdmin
        {
            get
            {
                if (Session["SessionLoginAdmin"] == null)
                {
                    return null;
                }
                else
                {
                    return (Model.Admin)Session["SessionLoginAdmin"];
                }
            }
            set
            {
                Session["SessionLoginAdmin"] = value;
            }
        }
        /// <summary>
        /// 返回的Json数据
        /// </summary>
        public Model.JsonData json;
        /// <summary>
        /// 登录验证、权限验证
        /// </summary>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string url = filterContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
            if (LoginAdmin == null)
            {
                if (url.IndexOf("Login") < 0)
                {
                    filterContext.HttpContext.Response.Clear();
                    filterContext.HttpContext.Response.Write("<script>window.parent.parent.parent.location='/Home/Login';</script>");
                    filterContext.HttpContext.Response.End();
                }
            }
            ViewBag.LoginAdmin = LoginAdmin;
            ViewBag.Title = "礼多娇控制中心";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="returnxml">返回xml字符串</param>
        /// <param name="jrBehavior"></param>
        /// <returns></returns>
        public ActionResult Json(object obj ,int number, JsonRequestBehavior jrBehavior = JsonRequestBehavior.AllowGet)
        {
            Newtonsoft.Json.Converters.IsoDateTimeConverter jsonsettingconvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsonsettingconvert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            Newtonsoft.Json.JsonSerializerSettings jsonsetting = new Newtonsoft.Json.JsonSerializerSettings();
            jsonsetting.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            jsonsetting.Converters.Add(jsonsettingconvert);

            var str = Newtonsoft.Json.JsonConvert.SerializeObject(obj, jsonsetting);
            return Content(str, "text/json", UTF8Encoding.UTF8);//base.Json(obj, jrBehavior);
        }
    }
}
